﻿
// ResPkgDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ResPkg.h"
#include "ResPkgDlg.h"
#include "afxdialogex.h"
#include <ResPkgFile/ResPkgFile.h>
#include "ResPkgInsert.h"
#include "ResPkgEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CResPkgDlg 对话框



CResPkgDlg::CResPkgDlg(CWnd* pParent /*=nullptr*/)
    : CDialog(IDD_RESPKG_DIALOG, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR));
    CResPkgFile::Handle();
}

CResPkgDlg::~CResPkgDlg()
{
    CResPkgFile::Handle()->Release();
}

void CResPkgDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_ITEMS_LIST, m_listCtrl);
    DDX_Control(pDX, IDC_FILEPATH, m_filePath);
    DDX_Control(pDX, IDC_FILE_STATE, m_fileState);
    DDX_Control(pDX, IDC_PROGRESS_OPEN, m_progress);
}

BEGIN_MESSAGE_MAP(CResPkgDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_SIZE()
    ON_COMMAND(ID_FILE_NEW, &CResPkgDlg::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, &CResPkgDlg::OnFileOpen)
    ON_COMMAND(ID_FILE_SAVE, &CResPkgDlg::OnFileSave)
    ON_COMMAND(ID_FILE_SAVEAS, &CResPkgDlg::OnFileSaveas)
    ON_COMMAND(ID_FILE_INSERT, &CResPkgDlg::OnFileInsert)
    ON_COMMAND(ID_FILE_DELETE, &CResPkgDlg::OnFileDelete)
    ON_COMMAND(ID_FILE_CLOSE, &CResPkgDlg::OnFileClose)
    ON_COMMAND(ID_FILE_EXIT, &CResPkgDlg::OnFileExit)
    ON_COMMAND(ID_HELP_ABOUT, &CResPkgDlg::OnHelpAbout)
    ON_WM_CLOSE()
    ON_COMMAND(ID_FILE_EDIT, &CResPkgDlg::OnFileEdit)
    ON_MESSAGE(WM_UPDATE_PROGRESS, &CResPkgDlg::OnUpdateProgress)
    ON_MESSAGE(WM_FINISH_OPEN, &CResPkgDlg::OnFinishOpen)
    ON_NOTIFY(NM_DBLCLK, IDC_ITEMS_LIST, &CResPkgDlg::OnNMDblclkItemsList)
    ON_WM_DROPFILES()
    ON_COMMAND(ID_FILE_EXPORT, &CResPkgDlg::OnFileExport)
END_MESSAGE_MAP()


// CResPkgDlg 消息处理程序

BOOL CResPkgDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。

    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != nullptr)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标

    GetWindowText(m_strTitle);
    m_listCtrl.SetExtendedStyle(m_listCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    m_listCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 30);
    m_listCtrl.InsertColumn(1, _T("File"), LVCFMT_LEFT, 90);
    m_listCtrl.InsertColumn(2, _T("Size"), LVCFMT_LEFT, 120);
    m_listCtrl.InsertColumn(3, _T("Level"), LVCFMT_LEFT, 60);
    m_listCtrl.InsertColumn(4, _T("Compressed size"));
    m_listCtrl.SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER);
    m_progress.SetRange(0, 100);
    m_progress.SetPos(0);

    return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CResPkgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CResPkgDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CResPkgDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


BOOL CResPkgDlg::PreTranslateMessage(MSG* pMsg)
{
    if (m_hAccel)
        if (TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
            return true;
    if (pMsg->message == WM_KEYDOWN)
        switch (pMsg->wParam)
        {
        case VK_ESCAPE:
        case VK_RETURN:
            return TRUE;
        default:
            break;
        }
    return CDialog::PreTranslateMessage(pMsg);
}


void CResPkgDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_ITEMS_LIST);
    if (pList)
    {
        RECT rtList;
        pList->GetWindowRect(&rtList);
        RECT rtDlg;
        GetWindowRect(&rtDlg);
        pList->SetWindowPos(nullptr, 0, 0, rtDlg.right - rtDlg.left - 2 * (rtList.left - rtDlg.left), rtDlg.bottom - rtDlg.top - (rtList.left - rtDlg.left) - (rtList.top - rtDlg.top), SWP_NOZORDER | SWP_NOMOVE);
        pList->SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
    }
}


void CResPkgDlg::OnFileNew()
{
    auto funcNew = [](CResPkgDlg* pDlg)->void {
        CResPkgFile::Handle()->New();
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVE, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVEAS, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_INSERT, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EXPORT, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EDIT, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_DELETE, MF_ENABLED);
        pDlg->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_CLOSE, MF_ENABLED);
        pDlg->m_filePath.SetWindowText(_T(""));
        CString strNewFileState;
        strNewFileState.LoadString(IDS_NEW_FILE_STATE);
        strNewFileState.Format(strNewFileState, 0);
        pDlg->m_fileState.SetWindowText(strNewFileState);
        pDlg->m_listCtrl.DeleteAllItems();
        pDlg->m_progress.SetPos(0);
    };
    if (!CResPkgFile::Handle()->IsChanged())
        funcNew(this);
    else
    {
        CString strSaveChange;
        strSaveChange.LoadString(IDS_SAVE_CHANGE);
        switch (MessageBox(strSaveChange, (LPCTSTR)0, MB_YESNOCANCEL))
        {
        case IDYES:
            if (FALSE == CResPkgDlg::_OnFileSave())
                break;
            funcNew(this);
            break;
        case IDNO:
            funcNew(this);
            break;
        case IDCANCEL:
        default:
            break;
        }
    }
}


void CResPkgDlg::OnFileOpen()
{
    if (CResPkgFile::Handle()->IsChanged())
    {
        CString strSaveChange;
        strSaveChange.LoadString(IDS_SAVE_CHANGE);
        switch (MessageBox(strSaveChange, (LPCTSTR)0, MB_YESNOCANCEL))
        {
        case IDYES:
            if (FALSE == CResPkgDlg::_OnFileSave())
                return;
            break;
        case IDNO:
            break;
        case IDCANCEL:
        default:
            return;
        }
    }
    CResPkgFile::Handle()->Close();
    CString strExt, strFilter;

    strExt.LoadString(IDS_EXTEND);
    strFilter.LoadString(IDS_FILTER);
    CFileDialog fileDlg(TRUE, strExt, (LPCTSTR)0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);
    if (IDOK == fileDlg.DoModal())
    {
        CResPkgFile::Handle()->Open_Threaded(fileDlg.GetPathName().GetString(), [&](int n, int p1, int p2)->void {
            switch (n)
            {
            case 0:
                this->SendMessage(WM_FINISH_OPEN, TRUE, 0);
                break;
            case 1:
                this->SendMessage(WM_FINISH_OPEN, FALSE, 0);
                break;
            case 2:
                this->SendMessage(WM_UPDATE_PROGRESS, p1, p2);
                break;
            }
        });
        SetWindowText(fileDlg.GetFileName() + _T(" - ") + m_strTitle);
        m_filePath.SetWindowText(fileDlg.GetPathName());
    }
}


void CResPkgDlg::OnFileClose()
{
    if (CResPkgFile::Handle()->IsChanged())
    {
        CString strSaveChange;
        strSaveChange.LoadString(IDS_SAVE_CHANGE);
        switch (MessageBox(strSaveChange, (LPCTSTR)0, MB_YESNOCANCEL))
        {
        case IDYES:
            if (FALSE == CResPkgDlg::_OnFileSave())
                return;
            break;
        case IDNO:
            break;
        case IDCANCEL:
        default:
            return;
        }
    }
    CResPkgFile::Handle()->Close();
    CResPkgFile::Handle()->New();
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVE, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVEAS, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_INSERT, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EXPORT, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EDIT, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_DELETE, MF_DISABLED);
    GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_CLOSE, MF_DISABLED);
    m_filePath.SetWindowText(_T(""));
    m_fileState.SetWindowText(_T(""));
    m_listCtrl.DeleteAllItems();
    SetWindowText(m_strTitle);
    m_filePath.SetWindowText(_T(""));
    m_progress.SetPos(0);
}


BOOL CResPkgDlg::_OnFileSave()
{
    if (CResPkgFile::Handle()->GetPath() == _T(""))
        return CResPkgDlg::_OnFileSaveas();
    else
        CResPkgFile::Handle()->Save();
    return TRUE;
}


void CResPkgDlg::OnFileSave()
{
    _OnFileSave();
}


BOOL CResPkgDlg::_OnFileSaveas()
{
    CString strExt, strFilter;
    strExt.LoadString(IDS_EXTEND);
    strFilter.LoadString(IDS_FILTER);
    CFileDialog fileDlg(FALSE, strExt, (LPCTSTR)0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);
    if (IDOK == fileDlg.DoModal())
    {
        CResPkgFile::Handle()->SetPath(fileDlg.GetPathName().GetString());
        CResPkgFile::Handle()->Save();
        SetWindowText(fileDlg.GetFileName() + _T(" - ") + m_strTitle);
        m_filePath.SetWindowText(fileDlg.GetPathName());
        return TRUE;
    }
    return FALSE;
}


void CResPkgDlg::OnFileSaveas()
{
    _OnFileSaveas();
}


void CResPkgDlg::OnFileInsert()
{
    if (m_listCtrl.GetItemCount() > CResPkgFile::MAX_FILES)
    {
        CString strErr;
        strErr.LoadString(IDS_FILES_LIMIT);
        MessageBox(strErr);
    }
    else
    {
        CResPkgInsert dlgInsert;
        if (IDOK == dlgInsert.DoModal())
        {
            bool bFlag = false;
            switch (CResPkgFile::Handle()->Insert(dlgInsert.m_strFilePath.GetString(), dlgInsert.m_nLevel, dlgInsert.m_strSubName.GetString()))
            {
            case 1:
            {
                CString strOverwrite;
                strOverwrite.LoadString(IDS_OVERWRITE);
                if (IDYES == MessageBox(strOverwrite, (LPCTSTR)0, MB_YESNO))
                    if (0 == CResPkgFile::Handle()->Insert(dlgInsert.m_strFilePath.GetString(), dlgInsert.m_nLevel, dlgInsert.m_strSubName.GetString(), true))
                        bFlag = true;
            }
            break;
            case 0:
            default:
                bFlag = true;
                break;
            }
            if (bFlag)
                RefreshList();
        }
    }
}


void CResPkgDlg::OnFileExport()
{
    POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
    if (pos == nullptr)
    {
        CString strErr;
        strErr.LoadString(IDS_DELETE_NONE);
        MessageBox(strErr);
    }
    else
    {
        int nIdx = m_listCtrl.GetNextSelectedItem(pos);
        if (nIdx >= 0 && nIdx < m_listCtrl.GetItemCount())
        {
            CString strFilter;
            strFilter.LoadString(IDS_ALL);
            auto strKey = m_listCtrl.GetItemText(nIdx, 1);
            CFileDialog fileDlg(FALSE, nullptr, m_listCtrl.GetItemText(nIdx, 1), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);
            if (IDOK == fileDlg.DoModal())
            {
                CFile file;
                file.Open(fileDlg.GetPathName(), CFile::modeCreate | CFile::modeWrite);
                auto f = CResPkgFile::Handle()->GetFiles()[CResPkgFile::Handle()->GetFilesIndices().at(strKey.GetString())];
                file.Write(f.vSource.data(), f.vSource.size());
                file.Close();
            }
        }
    }
}


void CResPkgDlg::OnFileEdit()
{
    POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
    if (pos == nullptr)
    {
        CString strErr;
        strErr.LoadString(IDS_DELETE_NONE);
        MessageBox(strErr);
    }
    else
    {
        int nIdx = m_listCtrl.GetNextSelectedItem(pos);
        if (nIdx >= 0 && nIdx < m_listCtrl.GetItemCount())
        {
            CResPkgEdit dlgEdit(_ttoi(m_listCtrl.GetItemText(nIdx, 3).GetString()), m_listCtrl.GetItemText(nIdx, 1));
            if (IDOK == dlgEdit.DoModal())
            {
                CResPkgFile::Handle()->Edit(dlgEdit.m_strOldSubName.GetString(), dlgEdit.m_nLevel, dlgEdit.m_strSubName.GetString());
                RefreshList();
            }
        }
    }
}


void CResPkgDlg::OnFileDelete()
{
    POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
    if (pos == nullptr)
    {
        CString strErr;
        strErr.LoadString(IDS_DELETE_NONE);
        MessageBox(strErr);
    }
    else
    {
        while (pos)
        {
            int nIdx = m_listCtrl.GetNextSelectedItem(pos);
            if (nIdx >= 0 && nIdx < m_listCtrl.GetItemCount())
                CResPkgFile::Handle()->Delete(m_listCtrl.GetItemText(nIdx, 1).GetString());
        }
        RefreshList();
    }
}


void CResPkgDlg::OnFileExit()
{
    if (CResPkgFile::Handle()->IsChanged())
    {
        CString strSaveChange;
        strSaveChange.LoadString(IDS_SAVE_CHANGE);
        switch (MessageBox(strSaveChange, (LPCTSTR)0, MB_YESNOCANCEL))
        {
        case IDYES:
            if (FALSE == CResPkgDlg::_OnFileSave())
                return;
            break;
        case IDNO:
            break;
        case IDCANCEL:
        default:
            return;
        }
    }
    CResPkgDlg::OnCancel();
}


void CResPkgDlg::OnHelpAbout()
{
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
}


void CResPkgDlg::OnClose()
{
    CResPkgDlg::OnFileExit();
}


afx_msg LRESULT CResPkgDlg::OnUpdateProgress(WPARAM wParam, LPARAM lParam)
{
    int p = int(double(wParam) / double(lParam) * 100 + 0.5);
    m_progress.SetPos(p);
    return 0;
}


afx_msg LRESULT CResPkgDlg::OnFinishOpen(WPARAM wParam, LPARAM lParam)
{
    if (wParam == TRUE)
    {
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVE, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_SAVEAS, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_INSERT, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EXPORT, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_EDIT, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_DELETE, MF_ENABLED);
        GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_FILE_CLOSE, MF_ENABLED);
        CString strNewFileState;
        strNewFileState.LoadString(IDS_NEW_FILE_STATE);
        auto& vFiles = CResPkgFile::Handle()->GetFiles();
        strNewFileState.Format(strNewFileState, vFiles.size());
        m_fileState.SetWindowText(strNewFileState);
        m_listCtrl.DeleteAllItems();
        RefreshList();
    }
    else
    {
        CResPkgDlg::OnFileClose();
        CString strErr;
        strErr.LoadString(IDS_OPEN_ERROR);
        MessageBox(strErr);
    }
    return 0;
}


void CResPkgDlg::OnNMDblclkItemsList(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
    if (pos == nullptr)
    {
        CString strErr;
        strErr.LoadString(IDS_DELETE_NONE);
        MessageBox(strErr);
    }
    else
    {
        int nIdx = m_listCtrl.GetNextSelectedItem(pos);
        if (nIdx >= 0 && nIdx < m_listCtrl.GetItemCount())
        {
            CResPkgEdit dlgEdit(_ttoi(m_listCtrl.GetItemText(nIdx, 3).GetString()), m_listCtrl.GetItemText(nIdx, 1));
            if (IDOK == dlgEdit.DoModal())
            {
                CResPkgFile::Handle()->Edit(dlgEdit.m_strOldSubName.GetString(), dlgEdit.m_nLevel, dlgEdit.m_strSubName.GetString());
                RefreshList();
            }
        }
    }
    *pResult = 0;
}


void CResPkgDlg::OnDropFiles(HDROP hDropInfo)
{
    UINT n;
    TCHAR szFilePath[MAX_PATH];
    n = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
    if (n > 0)
    {
        if (n > 1)
        {
            CString strErr;
            strErr.LoadString(IDS_DRAG_MORE);
            MessageBox(strErr);
        }
        DragQueryFile(hDropInfo, 0, szFilePath, sizeof(szFilePath));
        if (CResPkgFile::Handle()->IsChanged())
        {
            CString strSaveChange;
            strSaveChange.LoadString(IDS_SAVE_CHANGE);
            switch (MessageBox(strSaveChange, (LPCTSTR)0, MB_YESNOCANCEL))
            {
            case IDYES:
                if (FALSE == CResPkgDlg::_OnFileSave())
                    return;
                break;
            case IDNO:
                break;
            case IDCANCEL:
            default:
                return;
            }
        }
        CResPkgFile::Handle()->Close();
        CString strExt, strFilter;

        CString strFilePath = szFilePath;
        CString strFileName = szFilePath;
        for (auto i = strFilePath.GetLength() - 1; i >= 0; i--)
            if (strFilePath[i] == _T('\\'))
            {
                strFileName = strFilePath.Mid(i + 1);
                break;
            }
        CResPkgFile::Handle()->Open_Threaded(szFilePath, [&](UINT m, WPARAM p1, LPARAM p2)->void {
            switch (m)
            {
            case 0:
                this->SendMessage(WM_FINISH_OPEN, TRUE, 0);
                break;
            case 1:
                this->SendMessage(WM_FINISH_OPEN, FALSE, 0);
                break;
            case 2:
                this->SendMessage(WM_UPDATE_PROGRESS, p1, p2);
                break;
            }
        });
        SetWindowText(strFileName + _T(" - ") + m_strTitle);
        m_filePath.SetWindowText(szFilePath);
    }
    DragFinish(hDropInfo);
    CDialog::OnDropFiles(hDropInfo);
}


void CResPkgDlg::RefreshList()
{
    m_listCtrl.DeleteAllItems();
    auto vFiles = CResPkgFile::Handle()->GetFiles();
    auto mFilesIndices = CResPkgFile::Handle()->GetFilesIndices();
    for (size_t i = 0; i < vFiles.size(); i++)
    {
        CString strId;
        strId.Format(_T("%d"), int(i));
        m_listCtrl.InsertItem(int(i), strId);
        CString strSrcSize, strCompSize, strLevel;
        int nDepthSrcSize = 0, nDepthCompSize = 0;
        CString pstrPostfix[4] = { _T("B"), _T("KB"), _T("MB"), _T("GB") };
        double dSrcSize = double(vFiles[i].vSource.size());
        while (dSrcSize > 1024.0)
        {
            dSrcSize /= 1024.0;
            nDepthSrcSize++;
        }
        if (nDepthSrcSize != 0)
            strSrcSize.Format(_T("%.2lf %s"), dSrcSize, pstrPostfix[nDepthSrcSize]);
        else
            strSrcSize.Format(_T("%d %s"), int(dSrcSize), pstrPostfix[nDepthSrcSize]);
        double dCompSize = double(vFiles[i].vCompressed.size());
        while (dCompSize > 1024.0)
        {
            dCompSize /= 1024.0;
            nDepthCompSize++;
        }
        if (nDepthCompSize != 0)
            strCompSize.Format(_T("%.2lf %s"), dCompSize, pstrPostfix[nDepthCompSize]);
        else
            strCompSize.Format(_T("%d %s"), int(dCompSize), pstrPostfix[nDepthCompSize]);
        strLevel.Format(_T("%d"), vFiles[i].nLevel);
        m_listCtrl.SetItemText(int(i), 1, vFiles[i].strSubName.c_str());
        m_listCtrl.SetItemText(int(i), 2, strSrcSize);
        m_listCtrl.SetItemText(int(i), 3, strLevel);
        m_listCtrl.SetItemText(int(i), 4, strCompSize);
    }
    CString strNewFileState;
    strNewFileState.LoadString(IDS_NEW_FILE_STATE);
    strNewFileState.Format(strNewFileState, vFiles.size());
    m_fileState.SetWindowText(strNewFileState);
}
